import i18n from '@/i18n'
import { MetaFileDao } from '@apps/meta/domain/meta-file/meta-file.dao'
import { DomainModel } from '@apps/meta/domain/model'
import { Assist } from '@libs/lhy-common'
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { MetaFileBatchSaveDto } from './dto/meta-file-batch-save.dto'
import { MetaFileListGroupDto } from './dto/meta-file-list-group.dto'
import { MetaFileSaveDto } from './dto/meta-file-save.dto'
import { MetaFileTreeDto } from './dto/meta-File-tree.dto'
import { MetaFileUpdateDto } from './dto/meta-file-update.dto'
import { MetaFileService } from './meta-file.service'

@ApiTags(i18n.t('app-meta.meta-file.meta-file.12151166-0'))
@Controller({ path: 'file', version: '1' })
export class MetaFileController {
  constructor(
    private fileDao: MetaFileDao,
    private fileService: MetaFileService
  ) {}

  @ApiOperation({ summary: i18n.t('app-meta.meta-file.meta-file.12151166-1') })
  @Get('list_group')
  async listGroup(@Query() param: MetaFileListGroupDto) {
    return this.fileDao.listGroup(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-file.meta-file.12151166-2') })
  @Get('tree')
  async tree(@Query() param: MetaFileTreeDto) {
    return this.fileService.tree(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-file.meta-file.12151166-3') })
  @Put('save')
  async save(@Body() param: MetaFileSaveDto) {
    Assist.threw(
      await this.fileDao.isLabelExists([param]),
      i18n.t('app-meta.meta-file.meta-file.12151166-4', { args: [param.label] })
    )

    await this.fileDao.save({
      ...param,
      belong: DomainModel.MetaFileBelong.Platform,
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-file.meta-file.12151166-5') })
  @Put('batch_save')
  async batchSave(@Body() param: MetaFileBatchSaveDto) {
    if (param.cover) {
      await this.fileDao.batchUpsertOnUnique(param.files, DomainModel.MetaFileBelong.Platform)
    } else {
      Assist.threw(
        await this.fileDao.isLabelExists(
          param.files.map(file => ({
            platform: file.platform,
            groupLabel: file.groupLabel,
            label: file.label,
          }))
        ),
        i18n.t('app-meta.meta-file.meta-file.12151166-6')
      )

      await this.fileDao.batchSave(
        param.files.map(file => ({
          ...file,
          belong: DomainModel.MetaFileBelong.Platform,
        }))
      )
    }
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-file.meta-file.12151166-7') })
  @Post('update')
  async update(@Body() param: MetaFileUpdateDto) {
    const oldData = Assist.notNull(
      await this.fileDao.getById(param.id),
      i18n.t('app-meta.meta-file.meta-file.12151166-8', { args: [param.id] })
    )
    Assist.threw(
      oldData.belong === DomainModel.MetaFileBelong.System,
      i18n.t('app-meta.meta-file.meta-file.12151166-9')
    )
    Assist.threw(
      await this.fileDao.isLabelExists([{ ...param, platform: oldData.platform }]),
      i18n.t('app-meta.meta-file.meta-file.12151166-4', { args: [param.label] })
    )

    await this.fileDao.updateById(param.id, param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-file.meta-file.12151166-10') })
  @Delete('del')
  async del(@Query('id') id: number) {
    const oldData = Assist.notNull(
      await this.fileDao.getById(id),
      i18n.t('app-meta.meta-file.meta-file.12151166-11', { args: [id] })
    )
    Assist.threw(
      oldData.belong === DomainModel.MetaFileBelong.System,
      i18n.t('app-meta.meta-file.meta-file.12151166-12')
    )

    await this.fileDao.deleteById(id)
  }
}
